<template>
  <div>
    <td>
      <div class="my-tag" @dblclick="open">
        <input
          class="input"
          type="text"
          placeholder="输入标签"
          v-if="edit"
          v-focus
          @blur="close"
          @keydown.enter="close"
          v-model="tag"
        />
        <div v-else class="text" >{{ value }}</div>
      </div>
    </td>
  </div>
</template>

<script>
export default {
  props: ['value'],
  data() {
    return {
      // 控制input框的显示与隐藏
      edit: false,
      tag:''
    }
  },
  methods: {
    open() { //双击事件打开
      this.edit = true
      // 数据双向绑定后赋值给tag显示
      this.tag = this.value
    },
    close() { //失去焦点关闭
      this.edit = false
      // 通知父组件修改内容
      this.$emit('input', this.tag)
    },
  },
  //自定义指令 打开input框时立即获取焦点
  directives: {
    focus: {
      inserted(el) {
        el.focus()
      },
    },
  },
}
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
